#include "loadDllUtil.h"
#include <sstream>
using namespace std;

HMODULE_HANDLE loadDllUtil::gdl_Open(const char* plname)
{
#if defined(SYS_WINDOWS)
	return LoadLibraryA(plname);
#endif
#if defined(SYS_LINUX)
	return dlopen(plname, RTLD_NOW | RTLD_GLOBAL);
#endif
};

void loadDllUtil::gdl_Close(HMODULE_HANDLE h)
{
	if (h)
	{
#if defined(SYS_WINDOWS)
		FreeLibrary(h);
#endif
#if defined(SYS_LINUX)
		dlclose(h);
#endif
	}
};

void* loadDllUtil::gdl_GetProc(HMODULE_HANDLE h, const char* pfname)
{
	if (h)
	{
#if defined(SYS_WINDOWS)
		return (void*)GetProcAddress(h, pfname);
#endif
#if defined(SYS_LINUX)
		return dlsym(h, pfname);
#endif
	}
	return 0;
};

const char* loadDllUtil::gdl_GetLastError()
{
#if defined(SYS_WINDOWS)
	DWORD  err = ::GetLastError();
	string errStr = std::to_string(err);
	return errStr.c_str();
#endif
#if defined(SYS_LINUX)
	return dlerror();
#endif
}

